package com.link.base.base.product.service;

import com.link.base.base.product.dao.mybatis.mapper.MpShopCartMapper;
import com.link.base.base.product.dao.mybatis.mapper.ProdAttrMapper;
import com.link.base.base.product.dao.mybatis.mapper.ShopCartMapper;
import com.link.base.base.product.model.ProdAttr;
import com.link.base.base.product.model.ShopCart;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.database.DataSource;
import com.link.core.basic.database.DataSourceType;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 购物车
 */
@Service
public class ShopCartServiceImpl extends BasicServiceImpl<ShopCart> implements ShopCartService {

    @Resource
    private ShopCartMapper shopCartMapper;

    @Resource
    private MpShopCartMapper mpShopCartMapper;

    @Resource
    private ProdAttrMapper prodAttrMapper;

    @Override
    public BasicMapper<ShopCart> getBasicMapper() {
        return shopCartMapper;
    }

    @Override
    @Transactional(rollbackFor = BasicServiceException.class)
    public void insert(ShopCart entity) throws Exception {
        //不分页
        entity.setPageFlag(false);
        //不统计总数
        entity.setTotalFlag(false);
        List<ShopCart> list = shopCartMapper.queryByExamplePage(entity);
        if (list.size() == 0) {
            super.insert(entity);
        } else if (list.size() == 1 && entity.getEmpId().equals(list.get(0).getEmpId())
                && entity.getProdId().equals(list.get(0).getProdId())) {
            //如果购物车中已有该产品，则合并数量
            ShopCart vaildEntity = list.get(0);
            double formerQty = vaildEntity.getQty();
            double newQty = entity.getQty();
            vaildEntity.setQty(formerQty + newQty);
            shopCartMapper.qtyUpdate(vaildEntity);
            entity.setId(vaildEntity.getId());
        } else {
            throw new ServiceException("PUBLIC-024");
        }
    }

    @Override
    public void beforUpsert(ShopCart entity) throws Exception {
        super.beforUpsert(entity);
        double qty = entity.getQty();
        if (qty <= 0) {
            throw new ServiceException("PRODUCT-029");
        }
    }

    @Override
    public void qtyUpdate(ShopCart entity) throws Exception {
        checkId(entity);
        double qty = entity.getQty();
        if (qty <= 0) {
            throw new ServiceException("PRODUCT-029");
        }
        shopCartMapper.qtyUpdate(entity);
    }

    @Override
    public void deleteByEmpId(ShopCart entity) throws Exception {
        if (entity.getEmpId() == null) {
            throw new ServiceException("PUBLIC-062");
        }
        shopCartMapper.deleteByEmpId(entity);
    }

    @Override
    @DataSource(DataSourceType.RANDOM)
    public int queryEmpProdCount(ShopCart entity) throws Exception {
        if (entity.getEmpId() == null) {
            entity.setEmpId(UserUtil.getUser().getId());
        }
        if (entity.getEmpId() == null) {
            throw new ServiceException("PUBLIC-062");
        }

        if (MP.equals(entity.getLoginType())) {
            return mpShopCartMapper.queryEmpProdCount(entity);
        } else {
            return shopCartMapper.queryEmpProdCount(entity);
        }
    }

    @Override
    public int deleteByOrderId(ShopCart entity) throws Exception {
        if (StringUtils.isBlank(entity.getAttr1())) {
            throw new ServiceException("ORDER-018");
        }
        return shopCartMapper.deleteByOrderId(entity);
    }

    @Override
    public List<ShopCart> queryByExamplePage(ShopCart entity) throws Exception {
        String y = "Y";
        if (MP.equals(entity.getLoginType()) || y.equals(entity.getMpUser())) {
            return mpShopCartMapper.queryByExamplePage(entity);
        } else {
            return shopCartMapper.queryByExamplePage(entity);
        }
    }

    @Override
    public ShopCart queryById(ShopCart entity) throws Exception {
        if (MP.equals(entity.getLoginType())) {
            return mpShopCartMapper.queryById(entity);
        } else {
            return shopCartMapper.queryById(entity);
        }
    }

    @Override
    public Map<String, Object> queryByMpProdExamplePage(ShopCart entity) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<ShopCart> shopCartList = this.queryByExamplePage(entity);
        for (int i = 0;i < shopCartList.size();i++) {
            ShopCart shopCart = shopCartList.get(i);
            List<ProdAttr> prodAttrList = new ArrayList<>();
            ProdAttr prodAttr = new ProdAttr();
            prodAttr.setProdId(shopCart.getProdId());
            prodAttr.setPageFlag(false);
            prodAttrList = prodAttrMapper.queryByExamplePage(prodAttr);
            shopCart.setProdAttrList(prodAttrList);
        }
        result.put("rows",shopCartList);
        return result;
    }

    @Override
    public Map<String, Object> insertShopCart(ShopCart shopCart) throws Exception {
        Map<String, Object> map = new HashMap<>();
        ShopCart shopC = new ShopCart();
        shopC.setProdId(shopCart.getProdId());
        shopC.setAcctId(shopCart.getAcctId());
        shopC.setPageFlag(false);
        List<ShopCart> shopCartList = this.queryByMpExamplePage(shopC);
        // 存在记录，做更新
        if (shopCartList.size() > 0) {
            ShopCart sc = shopCartList.get(0);
            // 已删除标志
            String yFlag = "Y";
            if (yFlag.equals(sc.getTbIsDelete())) {
                sc.setTbIsDelete("N");
                sc.setQty(shopCart.getQty());
            } else {
                sc.setQty(shopCart.getQty() + sc.getQty());
            }
            sc.setSelected(shopCart.getSelected());
            this.update(sc);
        } else {
            //新的产品新增记录
            this.insert(shopCart);
        }

        return map;
    }

    @Override
    public List<ShopCart> queryByMpExamplePage(ShopCart shopC) throws Exception {
        return mpShopCartMapper.queryByMpExamplePage(shopC);
    }

    @Override
    public void selectedUpdate(ShopCart shopCart) throws Exception {
        //全选
        String totalSelected = "totalSelected";
        //单选
        String singleSelected = "singleSelected";
        //全不选
        String noSelected = "noSelected";
        Long acctId = UserUtil.getUser().getId();
        shopCart.setAcctId(acctId);
        //单选
        if (StringUtils.equals(shopCart.getSelectType(),singleSelected)) {
            mpShopCartMapper.selectedUpdate(shopCart);
            //全选
        } else if (StringUtils.equals(shopCart.getSelectType(),totalSelected)) {
            mpShopCartMapper.allSelectedUpdate(shopCart);
            //去掉勾选
        } else if (StringUtils.equals(shopCart.getSelectType(),noSelected)) {
            mpShopCartMapper.removeSelectedUpdate(shopCart);
        }

    }
}
